package com.wyd.System.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyd.System.Bean.*;
import com.wyd.System.Mapper.*;
import com.wyd.System.Service.StudentsService;
import com.wyd.handler.BusinessException;
import com.wyd.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 文永达
 * @since 2020-12-25
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {

    @Autowired(required = false)
    private StudentsMapper studentsMapper;

    @Autowired(required = false)
    private GradeMapper gradeMapper;

    @Autowired(required = false)
    private InstituteMapper instituteMapper;

    @Autowired(required = false)
    private SpecialtyMapper specialtyMapper;

    @Autowired(required = false)
    private ClazzMapper clazzMapper;

    @Override
    public IPage<Students> findStudentPage(Page<Students> page, QueryWrapper<Students> wrapper) {
        return this.baseMapper.findStudentPage(page,wrapper);
    }

    @Override
    public void addStudent(Students students) {
        //判断是否增加了同一个学生
        String studentId = students.getStuId();
        //获取年级
        Integer grade_id = students.getStuGrade();
        //获取学院
        Integer institute_id = students.getStuInstitute();
        //获取专业
        Integer specialty_id = students.getStuSpecialty();
        //获取班级
        Integer clazz_id = students.getStuClazz();

        QueryWrapper<Students> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",studentId);
        Integer count = this.baseMapper.selectCount(wrapper);
        if(count!=0){
            throw new BusinessException(ResultCode.STUDENT_ALREADY_EXIST.getCode(),
                    ResultCode.STUDENT_ALREADY_EXIST.getMessage());
        }
        //年级没有对应的处理
        Grade grade = gradeMapper.selectById(grade_id);
        if(grade==null){
            throw new BusinessException(ResultCode.GRADE_NOT_EXIST.getCode(),
                    ResultCode.GRADE_NOT_EXIST.getMessage());
        }
        //学院没有对应的处理
        Institute institute = instituteMapper.selectById(institute_id);
        if(institute==null){
            throw new BusinessException(ResultCode.INSTITUTE_NOT_EXIST.getCode(),
                    ResultCode.INSTITUTE_NOT_EXIST.getMessage());
        }
        //专业没有对应的处理
        Specialty specialty = specialtyMapper.selectById(specialty_id);
        if(specialty==null){
            throw new BusinessException(ResultCode.SPECIALTY_NOT_EXIST.getCode(),
                    ResultCode.SPECIALTY_NOT_EXIST.getMessage());
        }
        //班级没有对应的处理
        Clazz clazz = clazzMapper.selectById(clazz_id);
        if(clazz==null){
            throw new BusinessException(ResultCode.CLAZZ_NOT_EXIST.getCode(),
                    ResultCode.CLAZZ_NOT_EXIST.getMessage());
        }

        this.baseMapper.insert(students);
    }

    @Override
    public void deleteStudentById(Integer id) {
        Students students=studentsMapper.selectById(id);
        if(students==null){
            throw new BusinessException(ResultCode.STUDENTS_CAN_NOT_DELETE.getCode(),
                    ResultCode.STUDENTS_CAN_NOT_DELETE.getMessage());
        }
        studentsMapper.deleteById(id);
    }

    @Override
    public void updateStudentById(Integer id, Students students) {
        students = studentsMapper.selectById(id);
        if(students==null){
            throw new BusinessException(ResultCode.STUDENTS_CAN_NOT_UPDATE.getCode(),
                    ResultCode.STUDENTS_CAN_NOT_UPDATE.getMessage());
        }
        this.baseMapper.updateById(students);
    }
}
